package com.yunwushanhe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.CommentVO;
import com.yunwushanhe.bean.vo.PageVO;
import com.yunwushanhe.constant.Constant;
import com.yunwushanhe.enums.AppHttpCodeEnum;
import com.yunwushanhe.exception.BlogSystemException;
import com.yunwushanhe.mapper.CommentMapper;
import com.yunwushanhe.entity.Comment;
import com.yunwushanhe.mapper.UserMapper;
import com.yunwushanhe.service.CommentService;
import com.yunwushanhe.util.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author 抬头看看天
 * @since 2023-08-12 17:10:51
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseResult<PageVO<List<CommentVO>>> commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        qw.eq(Constant.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        qw.eq(Comment::getRootId, Constant.COMMENT_ROOT_ID);
        qw.eq(Comment::getType,commentType);

        Page<Comment> commentPage = new Page<>(pageNum,pageSize);
        page(commentPage,qw);
        System.out.println("=========================="+commentPage.getTotal()+"============================");
        List<CommentVO> commentVOS = getCommentVOList(commentPage.getRecords());

        for (CommentVO commentVO : commentVOS) {
            List<CommentVO> childrenS = getChildrenList(commentVO.getId());
            commentVO.setChildren(childrenS);
        }

        return ResponseResult.ok(new PageVO<>(commentVOS,commentPage.getTotal()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> addComment(Comment comment) {
        //评论内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new BlogSystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        System.out.println(comment.toString()+"==========================");
        save(comment);
        return ResponseResult.ok();
    }

    /**
     * 查询子评论是也应应该分页查询，这里没有，后面可以补充一下
     * @param id
     * @return
     */
    private List<CommentVO> getChildrenList(Long id) {
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        qw.eq(Comment::getRootId,id);
        qw.orderByAsc(Comment::getCreateTime);

        List<Comment> comments = list(qw);

        return getCommentVOList(comments);
    }

    private List<CommentVO> getCommentVOList(List<Comment> comments) {
        List<CommentVO> commentVOS = BeanCopyUtil.copyBeanList(comments, CommentVO.class);
        for (CommentVO commentVO : commentVOS) {
            String creatorNickName = userMapper.selectById(commentVO.getCreateBy()).getNickName();
            commentVO.setUsername(creatorNickName);

            if(commentVO.getToCommentId() != -1){
                String commentedNickName = userMapper.selectById(commentVO.getToCommentUserId()).getNickName();
                commentVO.setToCommentUserName(commentedNickName);
            }
        }
        return commentVOS;
    }
}

